package com.sensePlusVR.realGame;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import android.util.Log;

public class RealGameConfigV2 {
	private static final String TAG = "RealGameConfigV2";
	private int sourceType;
	private int configVersion;
	private String gameName;
	//private int actionsNr = 0;
	private String startVideo;
	private String endVideo;
	private SimapleAudio backAudio;
	private List<Action> actions;
	public RealGameConfigV2(String configfile){
		File config = new File(configfile);
		String dir = config.getParent() + File.separatorChar;
		//actionsNr = 0;
		JsonParser parser = new JsonParser();
		
		try {
			JsonObject json = (JsonObject)parser.parse(new FileReader(config));
			configVersion = json.get("version").getAsInt();
			sourceType = json.get("sourcetype").getAsInt();
			gameName = json.get("desc").getAsString();
			backAudio = new SimapleAudio(json.getAsJsonArray("audioback"), dir);
			startVideo = dir + json.get("startvideo").getAsString();
			endVideo = dir + json.get("endingvideo").getAsString();
			Log.e(TAG, "startVideo="+startVideo);
			actions = new ArrayList<Action>();
			JsonArray jactions = json.getAsJsonArray("action");
			for (int i = 0; i < jactions.size(); i++){
				Log.e(TAG, "actions "+i);
				JsonElement el = jactions.get(i);
				if (el == null || !el.isJsonObject()){
					continue;
				}
				JsonObject jaction = el.getAsJsonObject();
				if (jaction == null || !jaction.isJsonObject()){
					continue;
				}
				actions.add(new Action(jaction, dir));
				//actionsNr ++;
			}
			
		} catch (JsonIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public int version(){
		return configVersion;
	}
	
	public int sourceType(){
		return sourceType;
	}
	
	public String gameName(){
		return gameName;
	}
	
	public boolean supportVideo(){
		Action s = actions.get(0);
		if (s.toString() != null && s.video() != null)
			return true;
		return false;
	}
	
	public String getBackAudio(){
		return backAudio.getRandomAudio();
	}
	
	public String startVideo(){
		return startVideo;
	}
	
	public String endVideo(){
		return endVideo;
	}
	
	public List<Action> actions(){
		return actions;
	}
}
